<template>
  <fix-area ref="fixArea">
    <div style="width: 100%;height: 50px; display: flex; justify-content: flex-end; align-items: flex-start">
      <el-button type="primary" @click="createVisible = true">添加</el-button>
    </div>
    <div style="flex: 1; min-height: 0; width: 100%">
      <table-block :loading="loading" :data="noticeList">
        <table-column label="排序" width="120px" prop="sort" fixed="left" />
        <table-column label="公告名称" width="220px" prop="name" fixed="left" />
        <table-column label="公告图标" width="120px" prop="icon" :use-slot="true">
          <template #default="{row}">
            <div style="cursor: pointer" @click="$refs.fixArea.previewImg(row.icon)">
              <cell-media :url="row.icon" />
            </div>
          </template>
        </table-column>
        <table-column label="公告内容" min-width="300px" prop="value" />
        <table-column label="状态" width="120px" prop="status" :use-slot="true">
          <template #default="{row}">
            <el-tag :type="row.status === 0 ? 'success' : 'danger'" size="small">{{ row.status === 0 ? '显示' : '隐藏' }}</el-tag>
          </template>
        </table-column>
        <table-column label="操作" width="220px" :use-slot="true" fixed="right">
          <template #default="{row}">
            <el-button size="mini" type="warning" @click="editNotice(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteNotice(row.id)">删除</el-button>
          </template>
        </table-column>
      </table-block>
    </div>
    <pagination :current-page="form.page" :total="total" @current-change="currentChange" />
    <create v-if="createVisible" :visible="createVisible" @cancel="createVisible = false" @enter="createEnter" />
    <edit v-if="editVisible" :id="id" :visible="editVisible" @cancel="editVisible = false" @enter="editEnter" />
  </fix-area>
</template>
<script>
import Create from '@/views/home/notice/component/Create.vue'
import { noticeList, deleteNotice } from '@/api/home'
import Edit from '@/views/home/notice/component/Edit.vue'
export default {
  name: 'Notice',
  components: {
    Create, Edit
  },
  data() {
    return {
      createVisible: false,
      noticeList: [],
      form: {
        page: 1
      },
      total: 0,
      loading: false,
      editVisible: false,
      id: ''
    }
  },
  async mounted() {
    try {
      this.loading = true
      const noticeListRes = await noticeList(this.form)
      this.noticeList = noticeListRes.data.data
      this.total = noticeListRes.data.total
      this.loading = false
    } catch (error) {
      this.$errorMessage()
      this.loading = false
    }
  },
  methods: {
    async deleteNotice(id) {
      try {
        await this.$warningConfirm('确认删除?')
        try {
          this.loading = true
          await deleteNotice({ id })
          try {
            this.loading = true
            const noticeListRes = await noticeList(this.form)
            this.noticeList = noticeListRes.data.data
            this.total = noticeListRes.data.total
            this.loading = false
          } catch (error) {
            this.$errorMessage()
            this.loading = false
          }
        } catch (error) {
          this.$errorMessage()
          this.loading = false
        }
      } catch (error) {
        console.log('取消删除')
      }
    },
    async currentChange(event) {
      try {
        this.loading = true
        this.form.page = event
        const noticeListRes = await noticeList(this.form)
        this.noticeList = noticeListRes.data.data
        this.total = noticeListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    editNotice(id) {
      this.id = id
      this.editVisible = true
    },
    async editEnter() {
      this.editVisible = false
      try {
        this.loading = true
        const noticeListRes = await noticeList(this.form)
        this.noticeList = noticeListRes.data.data
        this.total = noticeListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async createEnter() {
      this.createVisible = false
      try {
        this.loading = true
        const noticeListRes = await noticeList(this.form)
        this.noticeList = noticeListRes.data.data
        this.total = noticeListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    }
  }
}
</script>
